// myself/enroll/active-details.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import formatDate from '../../utils/format_date';
import utils from '../../utils/utils';
import notice from '../../utils/notice.js';

const app = getApp();
let times = null;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        visibleLogin: false,
        visible: false,
        visibleOnline: false,
        agree: false,
        details: {},
        isSign: 0,
        myself: false,
        groupTimer: [],
        selectedTimerIds: [],
        count: 0,
        amount: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        notice.addNotice("UserInfo", this.refreshUserInfo, this);
        notice.addNotice("RefreshActiveTimer", this.refreshActiveTimer, this);
        notice.addNotice("GetGroupTimer", this.getGroupTimer, this);

        network.getJson(api.getActive, {
            id: options.id
        }).then(res => {
            res.data.images = res.data.images.split(',');
            times = res.data.bookingTimes;
            for (let i = 0; i < times.length; i++) {
                times[i].startTemp = new Date(times[i].startTime).getTime();
            }
            times.sort((a, b) => a.startTemp - b.startTemp);
            for (let i = 0; i < times.length; i++) {
                times[i].format = formatDate.formatWeek(times[i].startTime, times[i].endTime);
                times[i].format2 = formatDate.formatWeek3(times[i].startTime);
                times[i].selected = false;

                if (times[i].sumCount >= res.data.peopleNum) {
                    times[i].disable = true;
                    times[i].disableText = '已满';
                }
            }

            let user = app.globalData.userInfo;
            this.setData({
                details: res.data,
                myself: res.data.creator == user.id
            })

            this.getGroupTimer();
        })

        wx.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline']
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        notice.removeNotice("UserInfo", this);
        notice.removeNotice("RefreshActiveTimer", this);
        notice.removeNotice("GetGroupTimer", this);
        times = null;
    },

    onShareAppMessage() {
        let user = app.globalData.userInfo;
        return {
            title: this.data.details.name,
            imageUrl: this.data.details.images[0],
            path: `/home/active/active-details?id=${this.data.details.id}&parentId=${user.id}`
        }
    },

    refreshUserInfo() {
        let userInfo = app.globalData.userInfo;
        this.setData({
            userInfo
        })
    },

    getGroupTimer() {
        app.getUserInfo().then(user => {
            network.getJson(api.activePartTimes, {
                userId: user.id,
                activeId: this.data.details.id
            }).then(result => {
                let timers = result.data;
                for (let i = 0; i < times.length; i++) {
                    if (timers.indexOf(times[i].id) != -1) {
                        times[i].disable = true;
                    } else {
                        if (!times[i].disable) {
                            times[i].disable = false;
                        }
                    }
                    times[i].selected = false; // 初始未选中
                }
                this.setData({
                    groupTimer: utils.groupByData(times, 'format2')
                })
            })
        })
    },

    onShareTimeline() {
        let user = app.globalData.userInfo;
        return {
            title: this.data.details.name,
            imageUrl: this.data.details.images[0],
            path: `/home/active/active-details?id=${this.data.details.id}&parentId=${user.id}`
        }
    },

    callPhone(e) {
        wx.makePhoneCall({
            phoneNumber: this.data.details.phone
        })
    },

    navTap(e) {
        const {
            name,
            lng,
            lat
        } = e.currentTarget.dataset;
        wx.openLocation({
            name,
            latitude: lat,
            longitude: lng
        })
    },

    goSign() {
        wx.navigateTo({
            url: '/myself/enroll/sign?details=' + encodeURIComponent(JSON.stringify(this.data.details))
        })
    },

})